/**
 * Created by MyPC on 2017/6/12.
 */
let express = require('express');
let router = express.Router();
let partsGroupDao = require('../dao/parts-group');
let Page = require('../../common/page');
let resultJson = require('../../common/resultJson');

// 获取列表数据
router.get('/', function (req, res, next) {
  let page = Page(req);
  page.query.isMulti = req.query.isMulti;
  page.query.names = req.query.names;
  let parts = partsGroupDao.listByPage(page);
  parts.then(function (results, fields) {
    let counts = partsGroupDao.countByPage(page);
    counts.then(function (result, fields) {
      page.total = result[0].total;
      res.json(resultJson.create({
        'page': page,
        'list': results
      }));
    })
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'))
  });
});

router.post('/', function (req, res, next) {
  let param = {};
  let names = req.body.names;
  if (typeof names === 'string' && names.length > 0) {
    param.names = names;
  } else {
    res.json(resultJson.create('请填写名称'));
    return;
  }
  let isMulti = Number(req.body.isMulti);
  if (isMulti === 1 || isMulti === 0) {
    param.isMulti = isMulti;
  } else {
    res.json(resultJson.create('请选择该类型是否可以多选'));
    return;
  }
  let remarks = req.body.remarks;
  if (remarks !== undefined) {
    param.remarks = remarks;
  } else {
    param.remarks = '';
  }
  var parts = partsGroupDao.save(param)
  parts.then(function (results, fields) {
    res.json(resultJson.create(1, '保存成功', results.insertId));
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'))
  })
})

router.delete('/:id', function (req, res, next) {
  let id = Number(req.params.id);
  if(!(id>0)){
    res.json(resultJson.create('请选择一条数据后再删除'));
    return;
  }
  let parts = partsGroupDao.removeById(id);
  parts.then(function (results, fields) {
    if(results.changedRows>0){
      res.json(resultJson.create(1, 'success', results.changedRows));
    }else{
      res.json(resultJson.create('删除失败'));
    }
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'))
  })
})

router.put('/:id', function (req, res, next) {
  let id = Number(req.params.id);
  if(id<=0){
    res.json(resultJson.create('请选择一条数据后再修改'));
    return;
  }
  let param = {
    'id': id
  };
  let names = req.body.names;
  if (typeof names === 'string' && names.length > 0) {
    param.names = names;
  } else {
    res.json(resultJson.create('请填写名称'));
    return;
  }
  let isMulti = Number(req.body.isMulti);
  if (isMulti === 1 || isMulti === 0) {
    param.isMulti = isMulti;
  } else {
    res.json(resultJson.create('请选择该类型是否可以多选'));
    return;
  }
  let remarks = req.body.remarks;
  if (remarks !== undefined) {
    param.remarks = remarks;
  } else {
    param.remarks = '';
  }
  var parts = partsGroupDao.updateById(param)
  parts.then(function (results, fields) {
    if(results.changedRows>0){
      res.json(resultJson.create(1, '修改成功', results.changedRows));
    }else{
      res.json(resultJson.create('修改失败'));
    }
  }).catch(function (err) {
    console.log(err);
    res.json(resultJson.create('系统繁忙,请稍后再试'))
  })

})

module.exports = router;
